package com.cskaoyan.shopping.service.impl;

import com.cskaoyan.mall.dto.ClearCartItemRequest;
import com.cskaoyan.shopping.service.ICartOrderService;
import com.cskaoyan.shopping.service.ICartService;
import com.cskaoyan.shopping.service.impl.cache.CacheManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ICartOrderServiceImpl
 * @Description TODO
 * @Author XiaoWJ
 * @Date 2022/9/21 21:56
 **/
@Service
public class ICartOrderServiceImpl implements ICartOrderService {

    @Autowired
    ICartService iCartService;

    @Autowired
    CacheManager cacheManager;


    @Override
    public void clearCartItemByUserID(ClearCartItemRequest request) {
        Long userId = request.getUserId();
        List<Long> productIds = request.getProductIds();

        // 如果购物车中有商品Id,则从购物车中删除
        if (!CollectionUtils.isEmpty(productIds)) {

            Map map = cacheManager.checkMap(userId.toString());

            for (Long productId : productIds) {
                if (map.containsKey(productId.toString())) {
                    iCartService.deleteCartItem(userId, productId);
                }
            }
        }
    }
}
